package com.ma.rpc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

public class RpcFuture {
	
	private  RpcResponse result;
	ResponseCallback callback;
	
	public  RpcResponse getResult() {
		return result;
	}

	public  void setResult(RpcResponse resultt) {
		result = resultt;
	}

	public RpcFuture(ResponseCallback callback) {
		this.callback=callback;
	}
	
	
	public  Object getRpcResult() {
		return result.getResult();
	}
	
	
	public void start(RpcResponse resultt, ExecutorService work) {	
		setResult(resultt);
		CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {
			
			return getRpcResult();
			},work).whenComplete((e,v)->{
				if(e!=null) {
				callback.done(e);
				}
				if(v!=null) {
					callback.caught(v);
				}
			});
		
	}
}
